//
//  SmallDecisionsIAPHelper.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/6/14.
//

#import "SmallDecisionsIAPHelper.h"
#import "IAPHelper.h"
#import "IAPShare.h"

@implementation SmallDecisionsIAPHelper
+ (void)applePayIAPWithProductId:(NSString *)productId successBlock:(IAPBlocker __nullable)successBlock  failedBlock:(IAPBlocker __nullable)failedBlock {
    
    NSSet* productSets = [[NSSet alloc] initWithObjects:productId, nil];
    [IAPShare sharedHelper].iap = [[IAPHelper alloc] initWithProductIdentifiers:productSets];
    [[IAPShare sharedHelper].iap requestProductsWithCompletion:^(SKProductsRequest* request,SKProductsResponse* response)
     {
        if(response > 0 ) {
            SKProduct* product =[[IAPShare sharedHelper].iap.products firstObject];
            if(product) {
                [[IAPShare sharedHelper].iap buyProduct:product
                                           onCompletion:^(SKPaymentTransaction* trans){
                    if(trans.error) {
                        if(failedBlock) {
                            dispatch_async(dispatch_get_main_queue(), ^{
                                failedBlock([trans.error localizedDescription]);
                            });
                        }
                    } else if(trans.transactionState == SKPaymentTransactionStatePurchased) {
                        
                        [self applePayIAPcheckReceiptSuccessBlock:^(NSString *msg) {
                            if(successBlock) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    successBlock(msg);
                                });
                            }
                        } failedBlock:^(NSString *msg) {
                            if(failedBlock) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    failedBlock(msg);
                                });
                            }
                        }];
                    } else if(trans.transactionState == SKPaymentTransactionStateFailed) {
                        if(failedBlock) {
                            dispatch_async(dispatch_get_main_queue(), ^{
                                failedBlock(@"订阅失败,请重新尝试！");
                            });
                        }
                    }
                }];
            } else {
                if(failedBlock) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        failedBlock(@"抱歉,没有查询到内购产品！");
                    });
                }
            }
        } else {
            if(failedBlock) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failedBlock(@"抱歉,没有查询到内购产品！");
                });
            }
        }
    }];
}

+ (void)applePayIAPRestoreProductSuccessBlock:(IAPBlocker __nullable)successBlock failedBlock:(IAPBlocker __nullable)failedBlock {
    
    NSSet* dataSet = [[NSSet alloc] initWithObjects:@"com.SmallDecisions.luckyQuarter",@"com.SmallDecisions.luckyYear",@"com.SmallDecisions.luckMonth", nil];
    [IAPShare sharedHelper].iap = [[IAPHelper alloc] initWithProductIdentifiers:dataSet];
    [[IAPShare sharedHelper].iap restoreProductsWithCompletion:^(SKPaymentQueue *payment, NSError *error) {
        if (payment.transactions.count>0) {
            [self applePayIAPcheckReceiptSuccessBlock:^(NSString *msg) {
                if(successBlock) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        successBlock(msg);
                    });
                }
            } failedBlock:^(NSString *msg) {
                if(failedBlock) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        failedBlock(msg);
                    });
                }
            }];
        } else {
            if(failedBlock) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    failedBlock(@"您的Apple ID没有订阅过高级功能,请先去订阅。");
                });
            }
        }
    }];
}

+ (void)applePayIAPcheckReceiptSuccessBlock:(IAPBlocker __nullable)successBlock  failedBlock:(IAPBlocker __nullable)failedBlock {
    [[IAPShare sharedHelper].iap checkReceipt:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]] onCompletion:^(NSString *response, NSError *error) {
        if (!error) {
            [SmallDecisionsSingletonHelper shareInstance].isVIP = YES;
            if(successBlock) {
                successBlock(@"恭喜您,订阅成功！立即体验吧！");
            }
        } else {
            [SmallDecisionsSingletonHelper shareInstance].isVIP = NO;
            if(failedBlock) {
                failedBlock(@"抱歉,订阅失败！请重新尝试！");
            }
        }
    }];
}
@end
